<?php

//http://ama.guitartar.com/demo.php

$arq = file_get_contents('php://input');


if(isset($arq)){
        $data = trim($arq);

        $data = json_decode($data);
}else{
        die("error input");
}

$rel_str = "";
if( !isset($data->poiid) && !isset($data->users) ){
	$rel_str .= "error: poiid is null  or users is null";
	die($rel_str);
}

$conn = mysql_connect("sinadb.cupfdqlhvurq.us-east-1.rds.amazonaws.com","eric","038613206");

if (!$conn) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("sinadb",$conn);

$users = $data->users;
$poiid_all = $data->poiid;
$total_num = $data->total_number;


foreach($users as $user){
	
	$uid = $user->id;
	$screen_name= $user->screen_name;
	$province = $user->province;
	$city = $user->city;
	$location = $user->location;
	$gender = $user->gender;
	$created_at = strtotime($user->created_at);
	$verified = $user->verified;
	$verified_reason = $user->verified_reason;
	$allow_all_comment = $user->allow_all_comment;
	$allow_all_act_msg = $user->allow_all_act_msg;
	$mobile_phone = "";
	$status = "";
	$mobile_phone = "";
	if(isset($user->status)){
		$status = $user->status;
		$mobile_phone =  $status->source;
	}
	
	$lat = 0;
	$long = 0;
	$poiid = $poiid_all;
	$title = "";
	$type = "";
	if(isset($u->status->annotations)){
		$anno = $u->status->annotations;
		foreach($anno as $a){
			if(isset($a->place->poiid)){
				$poiid = $a->place->poiid;
				
			}
			if(isset($a->place->title)){
				$title = $a->place->title;
				
			}
			if(isset($a->place->lon)){
				$long = $a->place->lon;
				
			}
			if(isset($a->place->lat)){
				$lat = $a->place->lat;
				
			}
			if(isset($a->place->type)){
				$type = $a->place->type;
				
			}
		}
	}
		
	//查一下有没有这个UID存在，有的话，UPDATE.
	$sql = "select * from user_poiid where uid = '$uid'";
	$rel = mysql_query($sql,$conn);
	if(is_array(mysql_fetch_array($rel))){
		$rel_str .= "exist $uid ... ";
		continue;
	}else{
		//没发现这个UID, 插入
		$sql = "insert user_poiid (uid,longitude,lat,poiid, poiid_title,type,updatetime ) values('$uid',$long,$lat,'$poiid','$title','$type',now());";
		
		$rel = mysql_query($sql,$conn);
		
		if($rel == false){
			$rel_str .= "error when insert  $uid  $sql ...";
			continue;
		}

	}
	
	
	//查一下有没有这个UID存在，有的话，UPDATE.
	$sql = "select * from sina_users where uid = '$uid'";
	$rel = mysql_query($sql,$conn);
	if(is_array(mysql_fetch_array($rel))){
		$rel_str .= "exist $uid ... ";
		continue;
	}else{
		$sql = "insert sina_users (uid,screen_name,province,city,location,gender,created_at,verified,verified_reason,allow_all_comment,allow_all_act_msg,mobile_phone) values ('$uid','$screen_name','$province','$city','$location','$gender',$created_at,'$verified','$verified_reason','$allow_all_comment','$allow_all_act_msg','$mobile_phone') ;";

		$rel = mysql_query($sql,$conn);
	
		if($rel == false){
			$rel_str .= "error when insert  $uid  $sql ...";
			continue;
		}
	}

	

//$sql = "update sina_user_follow_friend(uid,followers_count,friends_count,bi_followers_count) values();";

	//查一下有没有这个UID存在，有的话，UPDATE.
	$sql = "select * from sina_user_status where uid = '$uid'";
	$rel = mysql_query($sql,$conn);
	if(is_array(mysql_fetch_array($rel))){
		$rel_str .= "exist $uid ... ";
		continue;
	}else{
		//没发现这个UID, 插入
		$sql = "insert sina_user_status (uid,status,last_text,create_at ,poiid,longitude,lat,source) values('$uid','',‘$status','$created_at','$poiid','$long','$lat','$mobile_phone');";
		
		$rel = mysql_query($sql,$conn);
		
		if($rel == false){
			$rel_str .= "error when insert  $uid  $sql ...";
			continue;
		}

	}

//$sql = "update sina_user_status (uid,status,last_text,create_at , poiid,longitude,lat,source) values();";



}

mysql_close($conn);

die($rel_str);


?>